package com.tpc.control.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.TimeZone;
import java.util.Vector;

import javax.el.ELContext;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIParameter;
import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlGraphicImage;
import javax.faces.component.html.HtmlOutputLink;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;
import javax.faces.convert.NumberConverter;
import javax.faces.el.MethodBinding;
import javax.faces.event.ActionEvent;
import javax.faces.event.MethodExpressionActionListener;
import javax.faces.model.SelectItem;
import javax.faces.webapp.AttributeTag;
import javax.persistence.EntityManager;

import org.ajax4jsf.component.html.HtmlAjaxCommandButton;
import org.ajax4jsf.component.html.HtmlAjaxCommandLink;
import org.ajax4jsf.component.html.HtmlAjaxSupport;
import org.ajax4jsf.renderkit.compiler.FacetElement;
import org.richfaces.component.html.HtmlColumn;
import org.richfaces.component.html.HtmlColumnGroup;
import org.richfaces.component.html.HtmlComponentControl;
import org.richfaces.component.html.HtmlDataTable;
import org.richfaces.component.html.HtmlExtendedDataTable;
import org.richfaces.component.html.HtmlScrollableDataTable;
import org.richfaces.component.html.HtmlSimpleTogglePanel;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;
import org.richfaces.model.ScrollableTableDataModel.SimpleRowKey;
import org.richfaces.model.selection.SimpleSelection;

import com.sun.faces.taglib.jsf_core.SetPropertyActionListenerImpl;
import com.tpc.control.jpa.CommonData;
import com.tpc.control.jpa.CommonDataDAO;
import com.tpc.control.jpa.EntityManagerHelper;
import com.tpc.control.jpa.Plant;
import com.tpc.control.jpa.PlantDAO;
import com.tpc.control.jpa.VariantReport;
import com.tpc.control.jpa.VariantReportDAO;
import com.tpc.control.util.Formatting;

public class Con010 {
	private String plantFrom;
	private String plantTo;
	private String divisionFrom;
	private String divisionTo;
	private String productionFrom;
	private String productionTo;
	private String materialFrom;
	private String materialTo;
	private Date createDateFrom;
	private Date createDateTo;
	private Date planStartDateFrom;
	private Date planStartDateTo;

	private String customerFrom;
	private String customerTo;
	private String saleOrderFrom;
	private String saleOrderTo;
	private Date saleDueDateFrom;
	private Date saleDueDateTo;
	private String customerPoFrom;
	private String customerPoTo;

	private String pirNoFrom;
	private String pirNoTo;
	private Date pirDueDateFrom;
	private Date pirDueDateTo;

	private boolean selectSale = false;
	private boolean selectPir = false;
	private String option = "a"; // a=All, w=Warning
	private boolean selectCustomer = true;
	private boolean selectCustomerName = true;
	private boolean selectCustomerPoNo = true;
	private boolean selectSaleOrder = true;
	private boolean selectSaleLine = true;
	private boolean selectChannel = true;
	private boolean selectSaleCreated = true;
	private boolean selectPreDue = true;
	private boolean selectDueDate = true;
	private boolean selectPirNo = true;
	private boolean selectPirDueDate = true;
	private boolean selectMaterial = true;
	private boolean selectDesignFg = true;
	private boolean selectDyeLot = true;
	private boolean selectQuantity = true;
	private boolean selectUom = true;
	private boolean selectSeq = true;
	private boolean selectProdCreateDate = true;
	private boolean selectPlanStartDate = true;
	private boolean selectPlanFinishDate = true;
	private boolean selectStartDate = true;
	private boolean selectLastPostingDate = true;
	private boolean selectPoNo = true;
	private boolean selectDesignGreige = true;
	private boolean selectGreigeDue = true;
	private boolean selectPlanGreige = true;
	private boolean selectActualGreige = true;
	private boolean selectGreigeRemark = true;
	private boolean selectStartDye = true;
	private boolean selectDyeRemark = true;
	private boolean selectDueSubmitDate = true;
	private boolean selectSubmitDate = true;
	private boolean selectStatus = true;
	private boolean selectPlanDueDate = true;
	private boolean selectCurrentOper = true;
	private boolean selectUserStatus = true;
	private boolean selectTotalProduct = true;
	private boolean selectFinishGoods = true;
	private boolean selectConfirmToPlan = true;
	private boolean selectConfirmToSale = true;
	private boolean selectConfirmType = true;

	private List<CommonData> outputList;

	private HtmlDataTable dataTable = new HtmlDataTable();
	// private HtmlColumnGroup headerGroup;
	// private HtmlColumnGroup detailGroup;
	private Integer totalLine;
	private HtmlScrollableDataTable productionTable = new HtmlScrollableDataTable();
	private String variantName = "";
	private String selectVariantName = "";
	private Vector variantList = new Vector();
//	private HashMap status;

	private SimpleSelection selection = new SimpleSelection();
	private ArrayList<CommonData> selectedCommons = new ArrayList<CommonData>();
	private CommonData selectedCommon;
	// private Vector routings;
	private String height = "750";
	private Integer totalRed;
	private Integer totalOrange;
	private Integer totalYellow;
	private Integer totalPink;
	private Integer totalViolet;
	private Integer totalWhite;
	private Integer totalGreen;
	private String customerFilterValue;
	private String numRow = "35";
	private Date lastUpdate;
	private Vector saleProductions;
	private ExtendedTableDataModel<CommonData> dataModel;
	private List<CommonData> saleCommonDatas = new ArrayList<CommonData>();
	private CommonData selectSaleCommonData;
	private Integer totSaleProdLine;
	private Double totSaleProdQty;
	private Double totSaleProdWipQty;
	private Double totSaleProdGoodsQty;
	
	public Integer getTotSaleProdLine() {
		return totSaleProdLine;
	}

	public Double getTotSaleProdQty() {
		return totSaleProdQty;
	}

	public Double getTotSaleProdWipQty() {
		return totSaleProdWipQty;
	}

	public Double getTotSaleProdGoodsQty() {
		return totSaleProdGoodsQty;
	}
	
	public CommonData getSelectSaleCommonData() {
		return selectSaleCommonData;
	}

	public void setSelectSaleCommonData(CommonData selectSaleCommonData) {
		this.selectSaleCommonData = selectSaleCommonData;
	}

	public ExtendedTableDataModel<CommonData> getSaleCommonDatasDataModel() {
//		if (dataModel == null) {
			dataModel = new ExtendedTableDataModel<CommonData>(
					new DataProvider<CommonData>() {
						private static final long serialVersionUID = 5054087821033164847L;

						public CommonData getItemByKey(Object key) {
							for (CommonData c : saleCommonDatas) {
								if (key.equals(getKey(c))) {
									return c;
								}
							}
							return null;
						}

						public List<CommonData> getItemsByRange(int firstRow,
								int endRow) {
							return saleCommonDatas.subList(firstRow, endRow);
						}

						public Object getKey(CommonData item) {
							return item.getId();
						}

						public int getRowCount() {
							return saleCommonDatas.size();
						}

					});
//		}
		return dataModel;
	}

	public void setSaleCommonDatas(List<CommonData> saleCommonDatas) {
		this.saleCommonDatas = saleCommonDatas;
	}
	
	public List<CommonData> getSaleCommonDatas() {
		return saleCommonDatas;
	}

	public Vector getSaleProductions() {
		System.out.println("saleProductions");
		return saleProductions;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public String getNumRow() {
		return numRow;
	}

	public void setNumRow(String numRow) {
		this.numRow = numRow;
	}

	public String getCustomerFilterValue() {
		return customerFilterValue;
	}

	public void setCustomerFilterValue(String customerFilterValue) {
		this.customerFilterValue = customerFilterValue;
	}

	public Integer getTotalRed() {
		return totalRed;
	}

	public Integer getTotalOrange() {
		return totalOrange;
	}

	public Integer getTotalYellow() {
		return totalYellow;
	}

	public Integer getTotalPink() {
		return totalPink;
	}

	public Integer getTotalViolet() {
		return totalViolet;
	}

	public Integer getTotalWhite() {
		return totalWhite;
	}

	public Integer getTotalGreen() {
		return totalGreen;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	// public Vector getRoutings() {
	// return routings;
	// }
	//
	// public void setRoutings(Vector routings) {
	// this.routings = routings;
	// }

	public CommonData getSelectedCommon() {
		return selectedCommon;
	}

	public void setSelectedCommon(CommonData selectedCommon) {
		this.selectedCommon = selectedCommon;
	}

	public ArrayList<CommonData> getSelectedCommons() {
		return selectedCommons;
	}

	public void setSelectedCommons(ArrayList<CommonData> selectedCommons) {
		this.selectedCommons = selectedCommons;
	}

	public SimpleSelection getSelection() {
		return selection;
	}

	public void setSelection(SimpleSelection selection) {
		this.selection = selection;
	}

	public HtmlScrollableDataTable getProductionTable() {
		return productionTable;
	}

	public void setProductionTable(HtmlScrollableDataTable productionTable) {
		this.productionTable = productionTable;
	}

//	public HashMap getStatus() {
//		return status;
//	}
//
//	public void setStatus(HashMap status) {
//		this.status = status;
//	}

	public String getSelectVariantName() {
		return selectVariantName;
	}

	public void setSelectVariantName(String selectVariantName) {
		this.selectVariantName = selectVariantName;
	}

	public Vector getVariantList() {
		return variantList;
	}

	public String getVariantName() {
		return variantName;
	}

	public void setVariantName(String variantName) {
		this.variantName = variantName;
	}

	public Integer getTotalLine() {
		return totalLine;
	}

	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(HtmlDataTable dataTable) {
		this.dataTable = dataTable;
	}

	public boolean isSelectCustomer() {
		return selectCustomer;
	}

	public void setSelectCustomer(boolean selectCustomer) {
		this.selectCustomer = selectCustomer;
	}

	public boolean isSelectCustomerName() {
		return selectCustomerName;
	}

	public void setSelectCustomerName(boolean selectCustomerName) {
		this.selectCustomerName = selectCustomerName;
	}

	public boolean isSelectCustomerPoNo() {
		return selectCustomerPoNo;
	}

	public void setSelectCustomerPoNo(boolean selectCustomerPoNo) {
		this.selectCustomerPoNo = selectCustomerPoNo;
	}

	public boolean isSelectSaleOrder() {
		return selectSaleOrder;
	}

	public void setSelectSaleOrder(boolean selectSaleOrder) {
		this.selectSaleOrder = selectSaleOrder;
	}

	public boolean isSelectSaleLine() {
		return selectSaleLine;
	}

	public void setSelectSaleLine(boolean selectSaleLine) {
		this.selectSaleLine = selectSaleLine;
	}

	public boolean isSelectChannel() {
		return selectChannel;
	}

	public void setSelectChannel(boolean selectChannel) {
		this.selectChannel = selectChannel;
	}

	public boolean isSelectSaleCreated() {
		return selectSaleCreated;
	}

	public void setSelectSaleCreated(boolean selectSaleCreated) {
		this.selectSaleCreated = selectSaleCreated;
	}

	public boolean isSelectPreDue() {
		return selectPreDue;
	}

	public void setSelectPreDue(boolean selectPreDue) {
		this.selectPreDue = selectPreDue;
	}

	public boolean isSelectDueDate() {
		return selectDueDate;
	}

	public void setSelectDueDate(boolean selectDueDate) {
		this.selectDueDate = selectDueDate;
	}

	public boolean isSelectPirNo() {
		return selectPirNo;
	}

	public void setSelectPirNo(boolean selectPirNo) {
		this.selectPirNo = selectPirNo;
	}

	public boolean isSelectPirDueDate() {
		return selectPirDueDate;
	}

	public void setSelectPirDueDate(boolean selectPirDueDate) {
		this.selectPirDueDate = selectPirDueDate;
	}

	public boolean isSelectMaterial() {
		return selectMaterial;
	}

	public void setSelectMaterial(boolean selectMaterial) {
		this.selectMaterial = selectMaterial;
	}

	public boolean isSelectDesignFg() {
		return selectDesignFg;
	}

	public void setSelectDesignFg(boolean selectDesignFg) {
		this.selectDesignFg = selectDesignFg;
	}

	public boolean isSelectDyeLot() {
		return selectDyeLot;
	}

	public void setSelectDyeLot(boolean selectDyeLot) {
		this.selectDyeLot = selectDyeLot;
	}

	public boolean isSelectQuantity() {
		return selectQuantity;
	}

	public void setSelectQuantity(boolean selectQuantity) {
		this.selectQuantity = selectQuantity;
	}

	public boolean isSelectUom() {
		return selectUom;
	}

	public void setSelectUom(boolean selectUom) {
		this.selectUom = selectUom;
	}

	public boolean isSelectSeq() {
		return selectSeq;
	}

	public void setSelectSeq(boolean selectSeq) {
		this.selectSeq = selectSeq;
	}

	public boolean isSelectProdCreateDate() {
		return selectProdCreateDate;
	}

	public void setSelectProdCreateDate(boolean selectProdCreateDate) {
		this.selectProdCreateDate = selectProdCreateDate;
	}

	public boolean isSelectPlanStartDate() {
		return selectPlanStartDate;
	}

	public void setSelectPlanStartDate(boolean selectPlanStartDate) {
		this.selectPlanStartDate = selectPlanStartDate;
	}

	public boolean isSelectPlanFinishDate() {
		return selectPlanFinishDate;
	}

	public void setSelectPlanFinishDate(boolean selectPlanFinishDate) {
		this.selectPlanFinishDate = selectPlanFinishDate;
	}

	public boolean isSelectStartDate() {
		return selectStartDate;
	}

	public void setSelectStartDate(boolean selectStartDate) {
		this.selectStartDate = selectStartDate;
	}

	public boolean isSelectLastPostingDate() {
		return selectLastPostingDate;
	}

	public void setSelectLastPostingDate(boolean selectLastPostingDate) {
		this.selectLastPostingDate = selectLastPostingDate;
	}

	public boolean isSelectPoNo() {
		return selectPoNo;
	}

	public void setSelectPoNo(boolean selectPoNo) {
		this.selectPoNo = selectPoNo;
	}

	public boolean isSelectDesignGreige() {
		return selectDesignGreige;
	}

	public void setSelectDesignGreige(boolean selectDesignGreige) {
		this.selectDesignGreige = selectDesignGreige;
	}

	public boolean isSelectGreigeDue() {
		return selectGreigeDue;
	}

	public void setSelectGreigeDue(boolean selectGreigeDue) {
		this.selectGreigeDue = selectGreigeDue;
	}

	public boolean isSelectPlanGreige() {
		return selectPlanGreige;
	}

	public void setSelectPlanGreige(boolean selectPlanGreige) {
		this.selectPlanGreige = selectPlanGreige;
	}

	public boolean isSelectActualGreige() {
		return selectActualGreige;
	}

	public void setSelectActualGreige(boolean selectActualGreige) {
		this.selectActualGreige = selectActualGreige;
	}

	public boolean isSelectGreigeRemark() {
		return selectGreigeRemark;
	}

	public void setSelectGreigeRemark(boolean selectGreigeRemark) {
		this.selectGreigeRemark = selectGreigeRemark;
	}

	public boolean isSelectStartDye() {
		return selectStartDye;
	}

	public void setSelectStartDye(boolean selectStartDye) {
		this.selectStartDye = selectStartDye;
	}

	public boolean isSelectDyeRemark() {
		return selectDyeRemark;
	}

	public void setSelectDyeRemark(boolean selectDyeRemark) {
		this.selectDyeRemark = selectDyeRemark;
	}

	public boolean isSelectDueSubmitDate() {
		return selectDueSubmitDate;
	}

	public void setSelectDueSubmitDate(boolean selectDueSubmitDate) {
		this.selectDueSubmitDate = selectDueSubmitDate;
	}

	public boolean isSelectSubmitDate() {
		return selectSubmitDate;
	}

	public void setSelectSubmitDate(boolean selectSubmitDate) {
		this.selectSubmitDate = selectSubmitDate;
	}

	public boolean isSelectStatus() {
		return selectStatus;
	}

	public void setSelectStatus(boolean selectStatus) {
		this.selectStatus = selectStatus;
	}

	public boolean isSelectPlanDueDate() {
		return selectPlanDueDate;
	}

	public void setSelectPlanDueDate(boolean selectPlanDueDate) {
		this.selectPlanDueDate = selectPlanDueDate;
	}

	public boolean isSelectCurrentOper() {
		return selectCurrentOper;
	}

	public void setSelectCurrentOper(boolean selectCurrentOper) {
		this.selectCurrentOper = selectCurrentOper;
	}

	public boolean isSelectUserStatus() {
		return selectUserStatus;
	}

	public void setSelectUserStatus(boolean selectUserStatus) {
		this.selectUserStatus = selectUserStatus;
	}

	public boolean isSelectTotalProduct() {
		return selectTotalProduct;
	}

	public void setSelectTotalProduct(boolean selectTotalProduct) {
		this.selectTotalProduct = selectTotalProduct;
	}

	public boolean isSelectFinishGoods() {
		return selectFinishGoods;
	}

	public void setSelectFinishGoods(boolean selectFinishGoods) {
		this.selectFinishGoods = selectFinishGoods;
	}

	public boolean isSelectConfirmToPlan() {
		return selectConfirmToPlan;
	}

	public void setSelectConfirmToPlan(boolean selectConfirmToPlan) {
		this.selectConfirmToPlan = selectConfirmToPlan;
	}

	public boolean isSelectConfirmToSale() {
		return selectConfirmToSale;
	}

	public void setSelectConfirmToSale(boolean selectConfirmToSale) {
		this.selectConfirmToSale = selectConfirmToSale;
	}

	public boolean isSelectConfirmType() {
		return selectConfirmType;
	}

	public void setSelectConfirmType(boolean selectConfirmType) {
		this.selectConfirmType = selectConfirmType;
	}

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}

	public List<CommonData> getOutputList() {
		return outputList;
	}

	public boolean isSelectSale() {
		return selectSale;
	}

	public void setSelectSale(boolean selectSale) {
		this.selectSale = selectSale;
	}

	public boolean isSelectPir() {
		return selectPir;
	}

	public void setSelectPir(boolean selectPir) {
		this.selectPir = selectPir;
	}

	public String getCustomerPoFrom() {
		return Formatting.checkStringNull(customerPoFrom);
	}

	public void setCustomerPoFrom(String customerPoFrom) {
		this.customerPoFrom = customerPoFrom;
	}

	public String getCustomerPoTo() {
		return Formatting.checkStringNull(customerPoTo);
	}

	public void setCustomerPoTo(String customerPoTo) {
		this.customerPoTo = customerPoTo;
	}

	public String getProductionFrom() {
		return productionFrom;
	}

	public void setProductionFrom(String productionFrom) {
		this.productionFrom = productionFrom;
	}

	public String getProductionTo() {
		return productionTo;
	}

	public void setProductionTo(String productionTo) {
		this.productionTo = productionTo;
	}

	public String getPlantFrom() {
		return plantFrom;
	}

	public void setPlantFrom(String plantFrom) {
		this.plantFrom = plantFrom;
	}

	public String getPlantTo() {
		return plantTo;
	}

	public void setPlantTo(String plantTo) {
		this.plantTo = plantTo;
	}

	public String getDivisionFrom() {
		return divisionFrom;
	}

	public void setDivisionFrom(String divisionFrom) {
		this.divisionFrom = divisionFrom;
	}

	public String getDivisionTo() {
		return divisionTo;
	}

	public void setDivisionTo(String divisionTo) {
		this.divisionTo = divisionTo;
	}

	public String getMaterialFrom() {
		return materialFrom;
	}

	public void setMaterialFrom(String materialFrom) {
		this.materialFrom = materialFrom;
	}

	public String getMaterialTo() {
		return materialTo;
	}

	public void setMaterialTo(String materialTo) {
		this.materialTo = materialTo;
	}

	public Date getCreateDateFrom() {
		return createDateFrom;
	}

	public void setCreateDateFrom(Date createDateFrom) {
		this.createDateFrom = createDateFrom;
	}

	public Date getCreateDateTo() {
		return createDateTo;
	}

	public void setCreateDateTo(Date createDateTo) {
		this.createDateTo = createDateTo;
	}

	public Date getPlanStartDateFrom() {
		return planStartDateFrom;
	}

	public void setPlanStartDateFrom(Date planStartDateFrom) {
		this.planStartDateFrom = planStartDateFrom;
	}

	public Date getPlanStartDateTo() {
		return planStartDateTo;
	}

	public void setPlanStartDateTo(Date planStartDateTo) {
		this.planStartDateTo = planStartDateTo;
	}

	public String getCustomerFrom() {
		return Formatting.checkStringNull(customerFrom);
	}

	public void setCustomerFrom(String customerFrom) {
		this.customerFrom = customerFrom;
	}

	public String getCustomerTo() {
		return Formatting.checkStringNull(customerTo);
	}

	public void setCustomerTo(String customerTo) {
		this.customerTo = customerTo;
	}

	public String getSaleOrderFrom() {
		return Formatting.checkStringNull(saleOrderFrom);
	}

	public void setSaleOrderFrom(String saleOrderFrom) {
		this.saleOrderFrom = saleOrderFrom;
	}

	public String getSaleOrderTo() {
		return Formatting.checkStringNull(saleOrderTo);
	}

	public void setSaleOrderTo(String saleOrderTo) {
		this.saleOrderTo = saleOrderTo;
	}

	public Date getSaleDueDateFrom() {
		return saleDueDateFrom;
	}

	public void setSaleDueDateFrom(Date saleDueDateFrom) {
		this.saleDueDateFrom = saleDueDateFrom;
	}

	public Date getSaleDueDateTo() {
		return saleDueDateTo;
	}

	public void setSaleDueDateTo(Date saleDueDateTo) {
		this.saleDueDateTo = saleDueDateTo;
	}

	public String getPirNoFrom() {
		return Formatting.checkStringNull(pirNoFrom);
	}

	public void setPirNoFrom(String pirNoFrom) {
		this.pirNoFrom = pirNoFrom;
	}

	public String getPirNoTo() {
		return Formatting.checkStringNull(pirNoTo);
	}

	public void setPirNoTo(String pirNoTo) {
		this.pirNoTo = pirNoTo;
	}

	public Date getPirDueDateFrom() {
		return pirDueDateFrom;
	}

	public void setPirDueDateFrom(Date pirDueDateFrom) {
		this.pirDueDateFrom = pirDueDateFrom;
	}

	public Date getPirDueDateTo() {
		return pirDueDateTo;
	}

	public void setPirDueDateTo(Date pirDueDateTo) {
		this.pirDueDateTo = pirDueDateTo;
	}

	public String showReport() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		EntityManagerHelper.closeEntityManager();

		PlantDAO plantDao = new PlantDAO();
		List<Plant> plants = plantDao.findByOnLoading(plantFrom, plantTo, "X");
		if (plants.size() > 0) {
			String message = this.getMessageBundle()
					.getString("message_error1");
			FacesMessage facesMessage = new FacesMessage(message);
			facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
			facesContext.addMessage("con010Form", facesMessage);

			String plantText = "";
			for (int i = 0; i < plants.size(); i++) {
				if (i == 0) {
					plantText = plants.get(0).getPlantId();
				} else {
					plantText += " ," + plants.get(i).getPlantId();
				}
			}
			facesMessage = new FacesMessage(plantText);
			facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
			facesContext.addMessage("con010Form", facesMessage);
			return null;
		}

		CommonDataDAO commonDataDao = new CommonDataDAO();
		List<CommonData> outputTemp = commonDataDao.findByReport(option,
				selectSale, selectPir, plantFrom.toUpperCase(), plantTo
						.toUpperCase(), divisionFrom.toUpperCase(), divisionTo
						.toUpperCase(), productionFrom.toUpperCase(),
				productionTo.toUpperCase(), materialFrom.toUpperCase(),
				materialTo.toUpperCase(), createDateFrom, createDateTo,
				planStartDateFrom, planStartDateTo, getCustomerFrom()
						.toUpperCase(), getCustomerTo().toUpperCase(),
				getSaleOrderFrom().toUpperCase(), getSaleOrderTo()
						.toUpperCase(), saleDueDateFrom, saleDueDateTo,
				getCustomerPoFrom(), getCustomerPoTo(), getPirNoFrom()
						.toUpperCase(), getPirNoTo().toUpperCase(),
				pirDueDateFrom, pirDueDateTo);
//		status = new HashMap();
		outputList = new ArrayList();
		Calendar aCalendar = Calendar.getInstance(new Locale("th", "US"));
		Date runDate = aCalendar.getTime();
		totalRed = new Integer(0);
		totalOrange = new Integer(0);
		totalYellow = new Integer(0);
		totalPink = new Integer(0);
		totalViolet = new Integer(0);
		totalWhite = new Integer(0);
		totalGreen = new Integer(0);
		for (int i = 0; i < outputTemp.size(); i++) {
			CommonData aCommon = outputTemp.get(i);
			if (i == 0) {
				lastUpdate = new Date(aCommon.getPlant().getLastUpdate()
						.getTime());
			} else if (lastUpdate.after(new Date(aCommon.getPlant()
					.getLastUpdate().getTime()))) {
				lastUpdate = new Date(aCommon.getPlant().getLastUpdate()
						.getTime());
			}
			// System.out.print("Production: "+aCommon.getProductionNo());
			
			/* 
			 * delete this logic because this status was send from abap program
			 */
//			if (!aCommon.getUserStatus().equals("จบการผลิต")) {
//				// System.out.println("CreateDate: "+aCommon.getProdCreateDate());
//				Date startDate = aCommon.getStartDate();
//				if (aCommon.getStartDate() == null) {
//					startDate = runDate;
//				}
//				Date dueDate = new Date();
//				if (aCommon.getDueDate() != null
//						|| aCommon.getPirDueDate() != null) {
//					if (aCommon.getDueDate() != null) {
//						dueDate = aCommon.getDueDate();
//					} else {
//						dueDate = aCommon.getPirDueDate();
//					}
//					long diff1 = Math
//							.abs(dueDate.getTime() - runDate.getTime());
//					int dayDiff1 = (int) Math
//							.floor(diff1 / 1000 / 60 / 60 / 24);
//
//					long diff2 = Math.abs(aCommon.getPlanStartDate().getTime()
//							- runDate.getTime());
//					int dayDiff2 = (int) Math
//							.floor(diff2 / 1000 / 60 / 60 / 24);
//					if (startDate.after(dueDate)) {
//						status.put(aCommon.getId(), "red");
//						++totalRed;
//						outputList.add(aCommon);
//						continue;
//					} else if (dueDate.before(runDate)
//							&& aCommon.getFinishGoods() < aCommon.getQuantity()) {
//						status.put(aCommon.getId(), "orange");
//						++totalOrange;
//						outputList.add(aCommon);
//						continue;
//					} else if (dueDate.after(runDate)
//							&& dayDiff1 <= 3
//							&& (aCommon.getFinishGoods() < aCommon
//									.getQuantity() * 0.5)) {
//						status.put(aCommon.getId(), "yellow");
//						++totalYellow;
//						outputList.add(aCommon);
//						continue;
//					} else if (aCommon.getStartDate() == null
//							&& aCommon.getPlanStartDate().before(runDate)) {
//						status.put(aCommon.getId(), "pink");
//						++totalPink;
//						outputList.add(aCommon);
//						continue;
//					} else if (aCommon.getPlanStartDate().after(runDate)
//							&& dayDiff2 <= 3
//							&& (aCommon.getUserStatus().equals("รอผ้า")
//									|| aCommon.getUserStatus()
//											.equals("รอเกรย์")
//									|| aCommon.getUserStatus().equals(
//											"รอฟองน้ำ")
//									|| aCommon.getUserStatus().equals("รอโฟม") || aCommon
//									.getUserStatus().equals("รอผ้าดิบ"))) {
//						status.put(aCommon.getId(), "violet");
//						++totalViolet;
//						outputList.add(aCommon);
//						continue;
//					}
//				}
//			}
//			if (!status.containsKey(aCommon.getId())) {
//				if (option.equalsIgnoreCase("a")) {
//					if ((aCommon.getUserStatus().equals("จบการผลิต"))) {
//						status.put(aCommon.getId(), "green");
//						++totalGreen;
//						outputList.add(aCommon);
//					} else {
//						status.put(aCommon.getId(), "white");
//						++totalWhite;
//						outputList.add(aCommon);
//					}
//				} else if (option.equalsIgnoreCase("o")
//						&& !aCommon.getUserStatus().equals("จบการผลิต")) {
//					status.put(aCommon.getId(), "white");
//					++totalWhite;
//					outputList.add(aCommon);
//				}
//			}
			if (aCommon.getColorStatus().equalsIgnoreCase("red")) {
				++totalRed;
			} else if (aCommon.getColorStatus().equalsIgnoreCase("orange")) {
				++totalOrange;
			} else if (aCommon.getColorStatus().equalsIgnoreCase("yellow")) {
				++totalYellow;
			} else if (aCommon.getColorStatus().equalsIgnoreCase("pink")) {
				++totalPink;
			} else if (aCommon.getColorStatus().equalsIgnoreCase("violet")) {
				++totalViolet;
			} else if (aCommon.getColorStatus().equalsIgnoreCase("green")) {
				++totalGreen;
			} else {
				++totalWhite;
			}
			outputList.add(aCommon);
		}

		totalLine = outputList.size();
		generateDataTable();
		// generateProductionTable();
		return "con011";
	}

	private void generateProductionTable() {
		// productionTable = new HtmlScrollableDataTable();
		try {
			final Application application = FacesContext.getCurrentInstance()
					.getApplication();
			javax.faces.context.FacesContext conText = javax.faces.context.FacesContext
					.getCurrentInstance();

			productionTable = (HtmlScrollableDataTable) application
					.createComponent(HtmlScrollableDataTable.COMPONENT_TYPE);

			// options de la table
			productionTable.setRowKeyVar("rkv");
			productionTable.setFrozenColCount(4);
			productionTable.setHeight(this.getHeight());
			productionTable.setWidth("1250");
			productionTable.setId("productionList");
			// productionTable.setRows(outputList.size());
			productionTable.setVar("production");
			productionTable.setRows(totalLine);
			// productionTable.setHideWhenScrolling(true);
			// productionTable.setReRender("paginator");
			// productionTable.setSelection(selection);

			productionTable.setValueBinding("value", conText.getApplication()
					.createValueBinding("#{con010.outputList}"));
			productionTable.setSortMode("single");

			generateColumnTextWithId(conText, "statusColumn", "", "",
					"#{con010.status[production.id]}", "40px", "textCenter",
					false);
			generateColumnTextWithId(conText, "plantColumn", "Plant",
					"#{production.plant.plantId}", "40px", "textCenter", false);
			generateColumnTextWithId(conText, "divisionColumn", "Division",
					"#{production.division}", "40px", "textCenter", false);
			generateColumnProductionWithId(conText, "productionColumn",
					"Production No.", "#{production.id.productionNo}", "100px",
					"", false);

			if (this.isSelectCustomer())
				generateColumnTextWithId(conText, "customerColumn", this
						.getMessageBundle().getString("select01"),
						"#{production.customer}", "100px", "textCenter", false);
			if (this.isSelectCustomerName())
				generateColumnTextWithId(conText, "customerNameColumn", this
						.getMessageBundle().getString("select02"),
						"#{production.customerName}", "150px", "", false);
			if (this.isSelectCustomerPoNo())
				generateColumnTextWithId(conText, "customerPoColumn", this
						.getMessageBundle().getString("select03"),
						"#{production.customerPoNo}", "100px", "", false);
			if (this.isSelectSaleOrder())
				generateColumnTextWithId(conText, "saleOrderColumn", this
						.getMessageBundle().getString("select04"),
						"#{production.saleOrder}", "100px", "textCenter", false);
			if (this.isSelectSaleLine())
				generateColumnTextWithId(conText, "saleLineColumn", this
						.getMessageBundle().getString("select05"),
						"#{production.saleItem}", "80px", "textCenter", false);
			if (this.isSelectChannel())
				generateColumnTextWithId(conText, "channelColumn", this
						.getMessageBundle().getString("select06"),
						"#{production.distrChannel}", "40px", "textCenter",
						false);
			if (this.isSelectSaleCreated())
				generateColumnDateWithId(conText, "createdColumn", this
						.getMessageBundle().getString("select07"),
						"#{production.saleCreated}", "100px", "textCenter",
						false);
			if (this.isSelectPreDue())
				generateColumnDateWithId(conText, "preDueColumn", this
						.getMessageBundle().getString("select08"),
						"#{production.preDue}", "100px", "textCenter", false);
			if (this.isSelectDueDate())
				generateColumnDateWithId(conText, "dueDateColumn", this
						.getMessageBundle().getString("select09"),
						"#{production.dueDate}", "100px", "textCenter", false);
			if (this.isSelectPirNo())
				generateColumnTextWithId(conText, "pirColumn", this
						.getMessageBundle().getString("select10"),
						"#{production.pirNo}", "100px", "", false);
			if (this.isSelectPirDueDate())
				generateColumnDateWithId(conText, "pirDueColumn", this
						.getMessageBundle().getString("select11"),
						"#{production.pirDueDate}", "100px", "textCenter",
						false);
			if (this.isSelectMaterial())
				generateColumnTextWithId(conText, "materialColumn", this
						.getMessageBundle().getString("select12"),
						"#{production.material}", "120px", "", false);
			if (this.isSelectDesignFg())
				generateColumnTextWithId(conText, "designFGColumn", this
						.getMessageBundle().getString("select13"),
						"#{production.designNoFg}", "100px", "", false);
			if (this.isSelectDyeLot())
				generateColumnTextWithId(conText, "dyeLotColumn", this
						.getMessageBundle().getString("select14"),
						"#{production.dyelotLab}", "100px", "", false);
			if (this.isSelectQuantity())
				generateColumnNumberWithId(conText, "quantityColumn", this
						.getMessageBundle().getString("select15"),
						"#{production.quantity}", "100px", "textRight", false);
			if (this.isSelectUom())
				generateColumnTextWithId(conText, "uomColumn", this
						.getMessageBundle().getString("select16"),
						"#{production.uom}", "40px", "", false);
			if (this.isSelectSeq())
				generateColumnTextWithId(conText, "seqColumn", this
						.getMessageBundle().getString("select17"),
						"#{production.id.seq}", "40px", "textCenter", false);
			if (this.isSelectProdCreateDate())
				generateColumnDateWithId(conText, "prodCreateColumn", this
						.getMessageBundle().getString("select18"),
						"#{production.prodCreateDate}", "100px", "textCenter",
						false);
			if (this.isSelectPlanStartDate())
				generateColumnDateWithId(conText, "prodStartColumn", this
						.getMessageBundle().getString("select19"),
						"#{production.planStartDate}", "100px", "textCenter",
						false);
			if (this.isSelectPlanFinishDate())
				generateColumnDateWithId(conText, "planFinishColumn", this
						.getMessageBundle().getString("select20"),
						"#{production.planFinishDate}", "100px", "textCenter",
						false);
			if (this.isSelectStartDate())
				generateColumnDateWithId(conText, "startDateColumn", this
						.getMessageBundle().getString("select21"),
						"#{production.startDate}", "100px", "textCenter", false);
			if (this.isSelectLastPostingDate())
				generateColumnDateWithId(conText, "lastPostingColumn", this
						.getMessageBundle().getString("select22"),
						"#{production.lastPostingDate}", "100px", "textCenter",
						false);
			if (this.isSelectPoNo())
				generateColumnTextWithId(conText, "poNoColumn", this
						.getMessageBundle().getString("select23"),
						"#{production.poNo}", "100px", "", false);
			if (this.isSelectDesignGreige())
				generateColumnTextWithId(conText, "designGreigeColumn", this
						.getMessageBundle().getString("select24"),
						"#{production.designNoGreige}", "100px", "", false);
			if (this.isSelectGreigeDue())
				generateColumnDateWithId(conText, "greigeDueColumn", this
						.getMessageBundle().getString("select25"),
						"#{production.greigeDueDate}", "100px", "textCenter",
						false);
			if (this.isSelectPlanGreige())
				generateColumnTextWithId(conText, "planGreigeColumn", this
						.getMessageBundle().getString("select26"),
						"#{production.planGreige}", "100px", "textCenter",
						false);
			if (this.isSelectActualGreige())
				generateColumnDateWithId(conText, "actualGreigeColumn", this
						.getMessageBundle().getString("select27"),
						"#{production.actualGreige}", "100px", "textCenter",
						false);
			if (this.isSelectGreigeRemark())
				generateColumnTextWithId(conText, "greigeRemarkColumn", this
						.getMessageBundle().getString("select28"),
						"#{production.greigeRemark}", "100px", "", false);
			if (this.isSelectStartDye())
				generateColumnDateWithId(conText, "startDyeColumn", this
						.getMessageBundle().getString("select29"),
						"#{production.startDye}", "100px", "textCenter", false);
			if (this.isSelectDyeRemark())
				generateColumnTextWithId(conText, "dyeRemarkColumn", this
						.getMessageBundle().getString("select30"),
						"#{production.dyeRemark}", "100px", "", false);
			if (this.isSelectDueSubmitDate())
				generateColumnTextWithId(conText, "dueSubmitColumn", this
						.getMessageBundle().getString("select31"),
						"#{production.dueSubmitDate}", "100px", "textCenter",
						false);
			if (this.isSelectSubmitDate())
				generateColumnDateWithId(conText, "submitColumn", this
						.getMessageBundle().getString("select32"),
						"#{production.submitDate}", "100px", "textCenter",
						false);
			if (this.isSelectStatus())
				generateColumnTextWithId(conText, "statusItemColumn", this
						.getMessageBundle().getString("select33"),
						"#{production.status}", "100px", "", false);
			if (this.isSelectPlanDueDate())
				generateColumnDateWithId(conText, "planDueDateColumn", this
						.getMessageBundle().getString("select34"),
						"#{production.planDueDate}", "100px", "textCenter",
						false);
			if (this.isSelectCurrentOper())
				generateColumnTextWithId(conText, "currOperColumn", this
						.getMessageBundle().getString("select35"),
						"#{production.currentOperation}", "100px", "", false);
			if (this.isSelectUserStatus())
				generateColumnTextWithId(conText, "userStatusColumn", this
						.getMessageBundle().getString("select36"),
						"#{production.userStatus}", "100px", "", false);
			if (this.isSelectTotalProduct())
				generateColumnNumberWithId(conText, "totalProductionColumn",
						this.getMessageBundle().getString("select37"),
						"#{production.totProduceQty}", "100px", "textRight",
						false);
			if (this.isSelectFinishGoods())
				generateColumnNumberWithId(conText, "finishGoodsColumn", this
						.getMessageBundle().getString("select38"),
						"#{production.finishGoods}", "100px", "textRight",
						false);

			HtmlAjaxSupport ajaxSupport = new HtmlAjaxSupport();
			ajaxSupport.setId("take_selection");
			ajaxSupport.setReRender("routingOutputPanel");
			ajaxSupport.setEvent("onselectionchange");
			MethodExpression aMethodExpression = FacesContext
					.getCurrentInstance().getApplication()
					.getExpressionFactory().createMethodExpression(
							FacesContext.getCurrentInstance().getELContext(),
							"#{con010.showRouting}", String.class,
							new Class<?>[0]);

			ajaxSupport.setActionExpression(aMethodExpression);
			// Class args[] = { ActionEvent.class };
			// ajaxSupport.setActionListener(FacesContext.getCurrentInstance()
			// .getApplication().createMethodBinding(
			// "#{con010.showRoutingEvent}", args));
			productionTable.getFacets().put("a4jsupport", ajaxSupport);
			// productionTable.getChildren().add(ajaxSupport);
			// generateColumnRoutingButtonWithId(conText, "routingsColumn",
			// "Routing", "#{production.productionNo}", "100px", "textCenter");
			// generateColumnRoutingWithId(conText, "routingsColumn", "Routing",
			// "#{production.routings}", "100px", "textCenter");

			/*
			 * HtmlColumn aColumn = new HtmlColumn(); aColumn.setId("Titre");
			 * 
			 * HtmlOutputText aHeaderText = new HtmlOutputText();
			 * aHeaderText.setValue("Production No.");
			 * 
			 * HtmlOutputText aValueText = new HtmlOutputText();
			 * aValueText.setValueBinding("value", conText.getApplication()
			 * .createValueBinding("#{production.productionNo}"));
			 * aColumn.getChildren().add(aValueText);
			 * aColumn.setHeader(aHeaderText);
			 * this.productionTable.getChildren().add(aColumn);
			 */
			/*
			 * javax.faces.context.FacesContext conText =
			 * javax.faces.context.FacesContext .getCurrentInstance();
			 * productionTable.setValueBinding("value", conText.getApplication()
			 * .createValueBinding("#{con010.outputList}"));
			 * productionTable.setVar("prod");
			 * productionTable.setSortMode("single");
			 * productionTable.setWidth("400px"); productionTable.setRows(30);
			 * productionTable.setHideWhenScrolling(false);
			 * productionTable.setRendered(true);
			 * productionTable.setHeight("400px");
			 * 
			 * HtmlColumn aColumn = new HtmlColumn(); HtmlOutputText aHeaderText
			 * = new HtmlOutputText(); aHeaderText.setValue("Plant");
			 * aColumn.setHeader(aHeaderText); HtmlOutputText aText = new
			 * HtmlOutputText(); aText.setValueBinding("value",
			 * conText.getApplication()
			 * .createValueBinding("#{prod.plant.plantId}"));
			 * aColumn.getChildren().add(aText);
			 * productionTable.getChildren().add(aColumn);
			 * 
			 * aColumn = new HtmlColumn(); aHeaderText = new HtmlOutputText();
			 * aHeaderText.setValue("Production No.");
			 * aColumn.setHeader(aHeaderText); aText = new HtmlOutputText();
			 * aText.setValueBinding("value", conText.getApplication()
			 * .createValueBinding("#{item.productionNo}"));
			 * aColumn.getChildren().add(aText);
			 * productionTable.getChildren().add(aColumn);
			 */
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void generateColumnProductionWithId(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String width, String style,
			boolean sort) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		aColumn.setWidth(width);
		aColumn.setStyleClass(style);
		aColumn.setSortable(sort);
		// aColumn.setStyle("width:40px;");

		HtmlOutputText aHeaderText = new HtmlOutputText();
		aHeaderText.setValue(headerText);

		HtmlAjaxCommandLink aValueLink = new HtmlAjaxCommandLink();
		aValueLink.setId("productionLink");
		aValueLink.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));
		aValueLink.setReRender("routingOutputPanel");

		// MethodExpression aMethodExpression =
		// FacesContext.getCurrentInstance()
		// .getApplication().getExpressionFactory()
		// .createMethodExpression(
		// FacesContext.getCurrentInstance().getELContext(),
		// "#{con010.showRouting}", String.class, new Class<?>[0]);
		// MethodExpressionActionListener aActionListener = new
		// MethodExpressionActionListener(aMethodExpression);
		//
		// aValueLink.setActionExpression(aMethodExpression);
		// Class args[] = { ActionEvent.class };
		// aValueLink.setActionListener(FacesContext.getCurrentInstance()
		// .getApplication().createMethodBinding(
		// "#{con010.showRoutingEvent}", args));
		//		
		// UIParameter paramProduction = new UIParameter();
		// paramProduction.setName("productionNo");
		// ValueExpression ve =
		// FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
		// createValueExpression(FacesContext.getCurrentInstance().getELContext(),
		// "#{production.productionNo}", String.class);
		//		
		// paramProduction.setValueExpression("value", ve);
		// aValueLink.getChildren().add(paramProduction);

		HtmlComponentControl aControl = new HtmlComponentControl();
		aControl.setFor("routingPanel");
		aControl.setAttachTo("productionLink");
		aControl.setOperation("show");
		aControl.setEvent("onclick");
		aValueLink.getChildren().add(aControl);

		aColumn.getChildren().add(aValueLink);
		aColumn.setHeader(aHeaderText);
		this.productionTable.getChildren().add(aColumn);
	}

	private void generateColumnTextWithId(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String width, String style,
			boolean sort) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		aColumn.setWidth(width);
		aColumn.setStyleClass(style);
		aColumn.setSortable(sort);
		// aColumn.setStyle("width:40px;");

		HtmlOutputText aHeaderText = new HtmlOutputText();
		aHeaderText.setValue(headerText);

		HtmlOutputText aValueText = new HtmlOutputText();
		aValueText.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));
		// aValueText.setStyle("width:40px");
		aColumn.getChildren().add(aValueText);
		aColumn.setHeader(aHeaderText);
		this.productionTable.getChildren().add(aColumn);
	}

	private void generateColumnTextWithId(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String color, String width,
			String style, boolean sort) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		aColumn.setWidth(width);
		aColumn.setStyleClass(style);
		aColumn.setSortable(sort);

		HtmlOutputText aHeaderText = new HtmlOutputText();
		aHeaderText.setValue(headerText);

		HtmlGraphicImage bImage = new HtmlGraphicImage();
		bImage.setValueBinding("value", conText.getApplication()
				.createValueBinding("/images/status/" + color + ".gif"));
		aColumn.getChildren().add(bImage);
		aColumn.setHeader(aHeaderText);
		this.productionTable.getChildren().add(aColumn);
	}

	private void generateColumnDateWithId(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String width, String style,
			boolean sort) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		aColumn.setWidth(width);
		aColumn.setStyleClass(style);
		aColumn.setSortable(sort);

		HtmlOutputText aHeaderText = new HtmlOutputText();
		aHeaderText.setValue(headerText);

		HtmlOutputText aValueText = new HtmlOutputText();
		aValueText.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));
		DateTimeConverter dateTimeConverter = new DateTimeConverter();
		dateTimeConverter.setPattern("dd.MM.yyyy");
		dateTimeConverter.setTimeZone(TimeZone.getDefault());
		dateTimeConverter.setLocale(new Locale("th", "US"));
		aValueText.setConverter(dateTimeConverter);
		aColumn.getChildren().add(aValueText);
		aColumn.setHeader(aHeaderText);
		this.productionTable.getChildren().add(aColumn);
	}

	private void generateColumnNumberWithId(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String width, String style,
			boolean sort) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		aColumn.setWidth(width);
		aColumn.setStyleClass(style);
		aColumn.setSortable(sort);

		HtmlOutputText aHeaderText = new HtmlOutputText();
		aHeaderText.setValue(headerText);

		HtmlOutputText aValueText = new HtmlOutputText();
		aValueText.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));
		NumberConverter numberConverter = new NumberConverter();
		numberConverter.setPattern("#,##0.00");
		aValueText.setConverter(numberConverter);
		aColumn.getChildren().add(aValueText);
		aColumn.setHeader(aHeaderText);
		this.productionTable.getChildren().add(aColumn);
	}

	private void generateColumnRoutingWithId(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String width, String style) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		aColumn.setWidth(width);
		aColumn.setStyleClass(style);

		HtmlOutputText aHeaderText = new HtmlOutputText();
		aHeaderText.setValue(headerText);

		// HtmlSimpleTogglePanel aPanel = new HtmlSimpleTogglePanel();
		// aPanel.setSwitchType("client");
		// aPanel.setOpened(false);

		HtmlDataTable routings = new HtmlDataTable();
		routings.setId("routingsTable");
		routings.setValueBinding("value", conText.getApplication()
				.createValueBinding("#{production.routings}"));
		routings.setVar("routing");

		HtmlColumn itemColumn1 = new HtmlColumn();
		HtmlOutputText headerText1 = new HtmlOutputText();
		headerText1.setValue("Operation");
		itemColumn1.setHeader(headerText1);
		HtmlOutputText itemText1 = new HtmlOutputText();
		itemText1.setValueBinding("value", conText.getApplication()
				.createValueBinding("#{routing['name']}"));
		itemColumn1.getChildren().add(itemText1);
		routings.getChildren().add(itemColumn1);

		HtmlColumn itemColumn2 = new HtmlColumn();
		HtmlOutputText headerText2 = new HtmlOutputText();
		headerText2.setValue("Data");
		itemColumn2.setHeader(headerText2);
		HtmlOutputText itemText2 = new HtmlOutputText();
		itemText2.setValueBinding("value", conText.getApplication()
				.createValueBinding("#{routing['data']}"));
		itemColumn2.getChildren().add(itemText2);
		routings.getChildren().add(itemColumn2);

		// aPanel.getChildren().add(routings);

		aColumn.getChildren().add(routings);
		aColumn.setHeader(aHeaderText);
		this.productionTable.getChildren().add(aColumn);
	}

	private void generateColumnRoutingButtonWithId(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String width, String style) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		aColumn.setWidth(width);
		aColumn.setStyleClass(style);

		HtmlOutputText aHeaderText = new HtmlOutputText();
		aHeaderText.setValue(headerText);

		MethodExpression aMethodExpression = FacesContext.getCurrentInstance()
				.getApplication().getExpressionFactory()
				.createMethodExpression(
						FacesContext.getCurrentInstance().getELContext(),
						"#{con010.showRouting}", String.class, new Class<?>[0]);

		HtmlAjaxCommandButton aButton = new HtmlAjaxCommandButton();
		aButton.setReRender("routingTable");
		aButton.setValue("Show");
		aButton.setId("showButton");
		aButton.setActionExpression(aMethodExpression);

		// aButton.setValueBinding("action", conText.getApplication()
		// .createValueBinding("#{con010.showRouting}"));
		aButton
				.setOncomplete("javascript:Richfaces.showModalPanel('routingPanel');");
		aButton.getAttributes().put("productionNo", valueBinding);
		aColumn.getChildren().add(aButton);
		aColumn.setHeader(aHeaderText);
		this.productionTable.getChildren().add(aColumn);
	}

	private void generateDataTable() {
		dataTable = new HtmlDataTable();

		try {
			javax.faces.context.FacesContext conText = javax.faces.context.FacesContext
					.getCurrentInstance();
			dataTable.setValueBinding("value", conText.getApplication()
					.createValueBinding("#{con010.outputList}"));
			dataTable.setVar("item");
			dataTable.setRows(Integer.valueOf(numRow));
			dataTable.setRowKeyVar("rkv");
			dataTable.setSortMode("multi");

			// headerGroup = new HtmlColumnGroup();
			// detailGroup = new HtmlColumnGroup();

			generateColumns();

			// HtmlAjaxSupport ajaxSupport = new HtmlAjaxSupport();
			// ajaxSupport.setId("take_selection");
			// ajaxSupport.setReRender("routingOutputPanel");
			// ajaxSupport.setEvent("onselectionchange");
			// MethodExpression aMethodExpression =
			// FacesContext.getCurrentInstance()
			// .getApplication().getExpressionFactory()
			// .createMethodExpression(
			// FacesContext.getCurrentInstance().getELContext(),
			// "#{con010.showRouting}", String.class, new Class<?>[0]);
			//
			// ajaxSupport.setActionExpression(aMethodExpression);
			// dataTable.getFacets().put("a4jsupport", ajaxSupport);

			// dataTable.getChildren().add(detailGroup);
			// dataTable.setHeader(headerGroup);
			// dataTable.setReRender("paginator");
			// dataTable.setId("commonTable");
			// dataTable.setRows(30);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void generateColumns() {
		javax.faces.context.FacesContext conText = javax.faces.context.FacesContext
				.getCurrentInstance();

//		generateColumnImage(conText, "statusColumn", "Status", "",
//				"#{con010.status[item.id]}", "textCenter");
		generateColumnImage(conText, "statusColumn", "Status", "",
				"#{item.colorStatus}", "textCenter");
		generateColumnText(conText, "plantColumn", "Plant",
				"#{item.plant.plantId}", "textCenter", false, false, "");
		generateColumnText(conText, "divisionColumn", "Division",
				"#{item.division}", "textCenter", false, false, "");
		generateColumnProductionLink(conText, "productionColumn",
				"Production No.", "#{item.id.productionNo}", "");

		if (this.isSelectCustomer())
			generateColumnText(conText, "customerColumn", this
					.getMessageBundle().getString("select01"),
					"#{item.customer}", "", true, false, "");
		if (this.isSelectCustomerName())
			generateColumnText(conText, "customerNameColumn", this
					.getMessageBundle().getString("select02"),
					"#{item.customerName}", "columnTextLeft", false, false, "");
		if (this.isSelectCustomerPoNo())
			generateColumnText(conText, "customerPoColumn", this
					.getMessageBundle().getString("select03"),
					"#{item.customerPoNo}", "", false, false, "");
		if (this.isSelectSaleOrder())
			generateColumnSaleOrderLink(conText, "saleOrderColumn", this
					.getMessageBundle().getString("select04"),
					"#{item.saleData.id.saleOrder}", "textCenter", true);
		if (this.isSelectSaleLine())
			generateColumnText(conText, "saleLineColumn", this
					.getMessageBundle().getString("select05"),
					"#{item.saleData.id.saleItem}", "textCenter", true, false,
					"");
		if (this.isSelectChannel())
			generateColumnText(conText, "channelColumn", this
					.getMessageBundle().getString("select06"),
					"#{item.distrChannel}", "textCenter", false, false, "");
		if (this.isSelectSaleCreated())
			generateColumnDate(conText, "createdColumn", this
					.getMessageBundle().getString("select07"),
					"#{item.saleCreated}", "textCenter", false);
		if (this.isSelectPreDue())
			generateColumnDate(conText, "preDueColumn", this.getMessageBundle()
					.getString("select08"), "#{item.preDue}", "textCenter",
					false);
		if (this.isSelectDueDate())
			generateColumnDate(conText, "dueDateColumn", this
					.getMessageBundle().getString("select09"),
					"#{item.dueDate}", "textCenter", true);
		if (this.isSelectPirNo())
			generateColumnText(conText, "pirColumn", this.getMessageBundle()
					.getString("select10"), "#{item.pirNo}", "", false, false,
					"");
		if (this.isSelectPirDueDate())
			generateColumnDate(conText, "pirDueColumn", this.getMessageBundle()
					.getString("select11"), "#{item.pirDueDate}", "textCenter",
					true);
		if (this.isSelectMaterial())
			generateColumnText(conText, "materialColumn", this
					.getMessageBundle().getString("select12"),
					"#{item.material}", "", false, false, "");
		if (this.isSelectDesignFg())
			generateColumnText(conText, "designFGColumn", this
					.getMessageBundle().getString("select13"),
					"#{item.designNoFg}", "columnTextLeft", false, false, "");
		if (this.isSelectDyeLot())
			generateColumnText(conText, "dyeLotColumn", this.getMessageBundle()
					.getString("select14"), "#{item.dyelotLab}", "", false,
					false, "");
		if (this.isSelectQuantity())
			generateColumnNumber(conText, "quantityColumn", this
					.getMessageBundle().getString("select15"),
					"#{item.quantity}", "textRight");
		if (this.isSelectUom())
			generateColumnText(conText, "uomColumn", this.getMessageBundle()
					.getString("select16"), "#{item.uom}", "", false, false, "");
		if (this.isSelectSeq())
			generateColumnText(conText, "seqColumn", this.getMessageBundle()
					.getString("select17"), "#{item.id.seq}", "textCenter",
					false, false, "");
		if (this.isSelectProdCreateDate())
			generateColumnDate(conText, "prodCreateColumn", this
					.getMessageBundle().getString("select18"),
					"#{item.prodCreateDate}", "textCenter", false);
		if (this.isSelectPlanStartDate())
			generateColumnDate(conText, "prodStartColumn", this
					.getMessageBundle().getString("select19"),
					"#{item.planStartDate}", "textCenter", false);
		if (this.isSelectPlanFinishDate())
			generateColumnDate(conText, "planFinishColumn", this
					.getMessageBundle().getString("select20"),
					"#{item.planFinishDate}", "textCenter", false);
		if (this.isSelectStartDate())
			generateColumnDate(conText, "startDateColumn", this
					.getMessageBundle().getString("select21"),
					"#{item.startDate}", "textCenter", false);
		if (this.isSelectLastPostingDate())
			generateColumnDate(conText, "lastPostingColumn", this
					.getMessageBundle().getString("select22"),
					"#{item.lastPostingDate}", "textCenter", false);
		if (this.isSelectPoNo())
			generateColumnText(conText, "poNoColumn", this.getMessageBundle()
					.getString("select23"), "#{item.poNo}", "", false, false,
					"");
		if (this.isSelectDesignGreige())
			generateColumnText(conText, "designGreigeColumn", this
					.getMessageBundle().getString("select24"),
					"#{item.designNoGreige}", "", false, false, "");
		if (this.isSelectGreigeDue())
			generateColumnDate(conText, "greigeDueColumn", this
					.getMessageBundle().getString("select25"),
					"#{item.greigeDueDate}", "textCenter", false);
		if (this.isSelectPlanGreige())
			generateColumnText(conText, "planGreigeColumn", this
					.getMessageBundle().getString("select26"),
					"#{item.planGreige}", "", false, false, "");
		if (this.isSelectActualGreige())
			generateColumnDate(conText, "actualGreigeColumn", this
					.getMessageBundle().getString("select27"),
					"#{item.actualGreige}", "textCenter", false);
		if (this.isSelectGreigeRemark())
			generateColumnText(conText, "greigeRemarkColumn", this
					.getMessageBundle().getString("select28"),
					"#{item.greigeRemark}", "", false, false, "");
		if (this.isSelectStartDye())
			generateColumnDate(conText, "startDyeColumn", this
					.getMessageBundle().getString("select29"),
					"#{item.startDye}", "textCenter", false);
		if (this.isSelectDyeRemark())
			generateColumnText(conText, "dyeRemarkColumn", this
					.getMessageBundle().getString("select30"),
					"#{item.dyeRemark}", "columnTextLeft", false, false, "");
		if (this.isSelectDueSubmitDate())
			generateColumnText(conText, "dueSubmitColumn", this
					.getMessageBundle().getString("select31"),
					"#{item.dueSubmitDate}", "textCenter", false, false, "");
		if (this.isSelectSubmitDate())
			generateColumnDate(conText, "submitColumn", this.getMessageBundle()
					.getString("select32"), "#{item.submitDate}", "textCenter",
					false);
		if (this.isSelectStatus())
			generateColumnText(conText, "statusItemColumn", this
					.getMessageBundle().getString("select33"),
					"#{item.status}", "", false, false, "");
		if (this.isSelectPlanDueDate())
			generateColumnDate(conText, "planDueDateColumn", this
					.getMessageBundle().getString("select34"),
					"#{item.planDueDate}", "textCenter", false);
		if (this.isSelectCurrentOper())
			generateColumnText(conText, "currOperColumn", this
					.getMessageBundle().getString("select35"),
					"#{item.currentOperation}", "columnTextLeft", false, false,
					"");
		if (this.isSelectUserStatus())
			generateColumnText(conText, "userStatusColumn", this
					.getMessageBundle().getString("select36"),
					"#{item.userStatus}", "columnTextLeft", false, false, "");
		if (this.isSelectTotalProduct())
			generateColumnNumber(conText, "totalProductionColumn", this
					.getMessageBundle().getString("select37"),
					"#{item.totProduceQty}", "textRight");
		if (this.isSelectFinishGoods())
			generateColumnNumber(conText, "finishGoodsColumn", this
					.getMessageBundle().getString("select38"),
					"#{item.finishGoods}", "textRight");
		if (this.isSelectConfirmToPlan())
			generateColumnDate(conText, "confirmToPlanColumn", this
					.getMessageBundle().getString("select39"),
					"#{item.confirmToPlan}", "textCenter", false);
		if (this.isSelectConfirmToSale())
			generateColumnDate(conText, "confirmToSaleColumn", this
					.getMessageBundle().getString("select40"),
					"#{item.confirmToSale}", "textCenter", false);
		if (this.isSelectConfirmType())
			generateColumnText(conText, "confirmTypeColumn", this
					.getMessageBundle().getString("select41"),
					"#{item.confirmType}", "textCenter", false, false, "");

		// generateColumnRouting(conText, "Routings", "#{item.finishGoods}");

	}

	private void generateColumnText(javax.faces.context.FacesContext conText,
			String columnId, String headerText, String valueBinding,
			String style, boolean isSort, boolean isFilter, String filterValue) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		HtmlOutputText aText = new HtmlOutputText();
		aText.setValue(headerText);
		aColumn.setHeader(aText);
		if (isSort) {
			aColumn.setValueBinding("sortBy", conText.getApplication()
					.createValueBinding(valueBinding));
		}
		if (isFilter) {
			aColumn.setValueBinding("filterBy", conText.getApplication()
					.createValueBinding(valueBinding));
			aColumn.setValueBinding("filterValue", conText.getApplication()
					.createValueBinding(filterValue));
			aColumn.setFilterEvent("onkeyup");

		}
		aColumn.setStyleClass(style);
		HtmlOutputText bText = new HtmlOutputText();
		bText.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));
		bText.setStyleClass(style);
		aColumn.getChildren().add(bText);
		dataTable.getChildren().add(aColumn);
	}

	private void generateColumnProductionLink(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String style) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		HtmlOutputText aText = new HtmlOutputText();
		aText.setValue(headerText);
		aColumn.setHeader(aText);

		aColumn.setStyleClass(style);
		HtmlAjaxCommandLink aValueLink = new HtmlAjaxCommandLink();
		aValueLink.setId("productionLink");
		aValueLink.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));
		aValueLink.setReRender("routingOutputPanel");

		ValueExpression target = conText.getApplication()
				.getExpressionFactory().createValueExpression(
						conText.getELContext(), "#{con010.selectedCommon}",
						CommonData.class);

		ValueExpression propertyValue = conText.getApplication()
				.getExpressionFactory().createValueExpression(
						conText.getELContext(), "#{item}", CommonData.class);

		SetPropertyActionListenerImpl listener = new SetPropertyActionListenerImpl(
				target, propertyValue);
		aValueLink.addActionListener(listener);

		// MethodExpression aMethodExpression =
		// FacesContext.getCurrentInstance()
		// .getApplication().getExpressionFactory()
		// .createMethodExpression(
		// FacesContext.getCurrentInstance().getELContext(),
		// "#{con010.getSelectedItem}", String.class, new Class<?>[0]);
		// aValueLink.setActionExpression(aMethodExpression);

		HtmlComponentControl aControl = new HtmlComponentControl();
		aControl.setFor("routingPanel");
		aControl.setAttachTo("productionLink");
		aControl.setOperation("show");
		aControl.setEvent("onclick");
		aValueLink.getChildren().add(aControl);

		aColumn.getChildren().add(aValueLink);
		dataTable.getChildren().add(aColumn);
	}

	private void generateColumnSaleOrderLink(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding, String style, boolean isSort) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		HtmlOutputText aText = new HtmlOutputText();
		aText.setValue(headerText);
		aColumn.setHeader(aText);
		if (isSort) {
			aColumn.setValueBinding("sortBy", conText.getApplication()
					.createValueBinding(valueBinding));
		}
		aColumn.setStyleClass(style);
		HtmlAjaxCommandLink aValueLink = new HtmlAjaxCommandLink();
		aValueLink.setId("saleOrderLink");
		aValueLink.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));
		aValueLink.setReRender("saleOrderOutputPanel");

		ValueExpression target = conText.getApplication()
				.getExpressionFactory().createValueExpression(
						conText.getELContext(), "#{con010.selectedCommon}",
						CommonData.class);
		ValueExpression propertyValue = conText.getApplication()
				.getExpressionFactory().createValueExpression(
						conText.getELContext(), "#{item}", CommonData.class);
		SetPropertyActionListenerImpl listener = new SetPropertyActionListenerImpl(
				target, propertyValue);
		aValueLink.addActionListener(listener);

		ValueExpression target2 = conText.getApplication()
		.getExpressionFactory().createValueExpression(
				conText.getELContext(), "#{con010.saleCommonDatas}",
				ArrayList.class);
		ValueExpression propertyValue2 = conText.getApplication()
				.getExpressionFactory().createValueExpression(
						conText.getELContext(), "#{item.saleData.commonDatasList}", ArrayList.class);
		SetPropertyActionListenerImpl listener2 = new SetPropertyActionListenerImpl(
				target2, propertyValue2);
		aValueLink.addActionListener(listener2);

		MethodExpression showSaleActionListener =
		conText.getApplication().getExpressionFactory().createMethodExpression(
		conText.getELContext(), "#{con010.showSaleEvent}", null, new Class[]
		{ActionEvent.class});
		aValueLink.addActionListener(new
		MethodExpressionActionListener(showSaleActionListener));

		// MethodExpression aMethodExpression =
		// FacesContext.getCurrentInstance()
		// .getApplication().getExpressionFactory()
		// .createMethodExpression(
		// FacesContext.getCurrentInstance().getELContext(),
		// "#{con010.getSelectedItem}", String.class, new Class<?>[0]);
		// aValueLink.setActionExpression(aMethodExpression);

		HtmlComponentControl aControl = new HtmlComponentControl();
		aControl.setFor("saleOrderPanel");
		aControl.setAttachTo("saleOrderLink");
		aControl.setOperation("show");
		aControl.setEvent("onclick");
		// aControl.setEvent("oncomplete");
		aValueLink.getChildren().add(aControl);

		aColumn.getChildren().add(aValueLink);
		dataTable.getChildren().add(aColumn);
	}

	private void generateColumnImage(javax.faces.context.FacesContext conText,
			String columnId, String headerText, String valueBinding,
			String color, String style) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		HtmlOutputText aText = new HtmlOutputText();
		aText.setValue(headerText);
		aColumn.setHeader(aText);

		aColumn.setStyleClass(style);
		// bColumn.setValueBinding("style", conText.getApplication()
		// .createValueBinding("background: "+color+";"));
		// HtmlOutputText bText = new HtmlOutputText();
		// bText.setValue("");
		// bText.setValueBinding("style", conText.getApplication()
		// .createValueBinding("background: "+color+";"));
		// bText.setStyle("background: "+color+";");
		HtmlGraphicImage bImage = new HtmlGraphicImage();
		bImage.setValueBinding("value", conText.getApplication()
				.createValueBinding("/images/status/" + color + ".gif"));
		aColumn.getChildren().add(bImage);
		dataTable.getChildren().add(aColumn);
	}

	private void generateColumnDate(javax.faces.context.FacesContext conText,
			String columnId, String headerText, String valueBinding,
			String style, boolean isSort) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		HtmlOutputText aText = new HtmlOutputText();
		aText.setValue(headerText);
		aColumn.setHeader(aText);
		if (isSort) {
			aColumn.setValueBinding("sortBy", conText.getApplication()
					.createValueBinding(valueBinding));
		}
		aColumn.setStyleClass(style);
		HtmlOutputText bText = new HtmlOutputText();
		bText.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));

		DateTimeConverter dateTimeConverter = new DateTimeConverter();
		dateTimeConverter.setPattern("dd.MM.yyyy");
		dateTimeConverter.setLocale(new Locale("th", "US"));
		dateTimeConverter.setTimeZone(TimeZone.getDefault());
		// dateTimeConverter.setTimeZone(new TimeZone("GMT"));
		bText.setConverter(dateTimeConverter);
		aColumn.getChildren().add(bText);
		dataTable.getChildren().add(aColumn);
	}

	private void generateColumnNumber(javax.faces.context.FacesContext conText,
			String columnId, String headerText, String valueBinding,
			String style) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		HtmlOutputText aText = new HtmlOutputText();
		aText.setValue(headerText);
		aColumn.setHeader(aText);

		aColumn.setStyleClass(style);
		HtmlOutputText bText = new HtmlOutputText();
		bText.setValueBinding("value", conText.getApplication()
				.createValueBinding(valueBinding));

		NumberConverter numberConverter = new NumberConverter();
		numberConverter.setPattern("#,##0.00");
		bText.setConverter(numberConverter);
		aColumn.getChildren().add(bText);
		dataTable.getChildren().add(aColumn);
	}

	private void generateColumnRouting(
			javax.faces.context.FacesContext conText, String columnId,
			String headerText, String valueBinding) {
		HtmlColumn aColumn = new HtmlColumn();
		aColumn.setId(columnId);
		HtmlOutputText aText = new HtmlOutputText();
		aText.setValue(headerText);
		aColumn.setHeader(aText);

		HtmlSimpleTogglePanel bPanel = new HtmlSimpleTogglePanel();
		bPanel.setSwitchType("client");
		bPanel.setOpened(false);

		HtmlDataTable routings = new HtmlDataTable();
		routings.setId("routingsTable");
		routings.setValueBinding("value", conText.getApplication()
				.createValueBinding("#{item.routings}"));
		routings.setVar("routing");

		HtmlColumn itemColumn1 = new HtmlColumn();
		HtmlOutputText headerText1 = new HtmlOutputText();
		headerText1.setValue("Operation");
		itemColumn1.setHeader(headerText1);
		HtmlOutputText itemText1 = new HtmlOutputText();
		itemText1.setValueBinding("value", conText.getApplication()
				.createValueBinding("#{routing['name']}"));
		itemColumn1.getChildren().add(itemText1);
		routings.getChildren().add(itemColumn1);

		HtmlColumn itemColumn2 = new HtmlColumn();
		HtmlOutputText headerText2 = new HtmlOutputText();
		headerText2.setValue("Data");
		itemColumn2.setHeader(headerText2);
		HtmlOutputText itemText2 = new HtmlOutputText();
		itemText2.setValueBinding("value", conText.getApplication()
				.createValueBinding("#{routing['data']}"));
		itemColumn2.getChildren().add(itemText2);
		routings.getChildren().add(itemColumn2);

		bPanel.getChildren().add(routings);
		aColumn.getChildren().add(bPanel);
		dataTable.getChildren().add(aColumn);
	}

	private void setSelectAllFieldsFromBoolean(boolean value) {
		this.setSelectCustomer(value);
		this.setSelectCustomerName(value);
		this.setSelectCustomerPoNo(value);
		this.setSelectSaleOrder(value);
		this.setSelectSaleLine(value);
		this.setSelectChannel(value);
		this.setSelectSaleCreated(value);
		this.setSelectPreDue(value);
		this.setSelectDueDate(value);
		this.setSelectPirNo(value);
		this.setSelectPirDueDate(value);
		this.setSelectMaterial(value);
		this.setSelectDesignFg(value);
		this.setSelectDyeLot(value);
		this.setSelectQuantity(value);
		this.setSelectUom(value);
		this.setSelectSeq(value);
		this.setSelectProdCreateDate(value);
		this.setSelectPlanStartDate(value);
		this.setSelectPlanFinishDate(value);
		this.setSelectStartDate(value);
		this.setSelectLastPostingDate(value);
		this.setSelectPoNo(value);
		this.setSelectDesignGreige(value);
		this.setSelectGreigeDue(value);
		this.setSelectPlanGreige(value);
		this.setSelectActualGreige(value);
		this.setSelectGreigeRemark(value);
		this.setSelectStartDye(value);
		this.setSelectDyeRemark(value);
		this.setSelectDueSubmitDate(value);
		this.setSelectSubmitDate(value);
		this.setSelectStatus(value);
		this.setSelectPlanDueDate(value);
		this.setSelectCurrentOper(value);
		this.setSelectUserStatus(value);
		this.setSelectTotalProduct(value);
		this.setSelectFinishGoods(value);
		this.setSelectConfirmToPlan(value);
		this.setSelectConfirmToSale(value);
		this.setSelectConfirmType(value);
	}

	public void deselectAll(ActionEvent event) {
		setSelectAllFieldsFromBoolean(false);
	}

	public void selectAll(ActionEvent event) {
		setSelectAllFieldsFromBoolean(true);
	}

	public String selectField() {
		variantName = "";
		generateVariantList();

		return "con012";
	}

	public String backtoSelectData() {
		return "con010";
	}

	public String saveVariant() {
		System.out.println("saveVariant");
		try {
			VariantReportDAO dao = new VariantReportDAO();
			EntityManagerHelper.beginTransaction();
			VariantReport aVariant = new VariantReport();
			aVariant.setName(variantName);
			aVariant.setSelectField(generateSelectFieldText());
			EntityManager em = EntityManagerHelper.getEntityManager();
			aVariant = em.merge(aVariant);
			// dao.save(variant);
			EntityManagerHelper.commit();

			generateVariantList();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public String takeSelection() {
		System.out.println("takeSelection");
		Iterator<Object> iterator = getSelection().getKeys();
		if (iterator.hasNext()) {
			SimpleRowKey key = (SimpleRowKey) iterator.next();
			selectSaleCommonData = saleCommonDatas.get(key.intValue());
		}
		return null;
	}

	public String showRouting() {
		System.out.println("showRouting");

		getSelectedCommons().clear();
		Iterator<Object> iterator = getSelection().getKeys();
		while (iterator.hasNext()) {
			SimpleRowKey key = (SimpleRowKey) iterator.next();
			getSelectedCommons().add(outputList.get(key.intValue()));
			System.out.println(key);
		}

		selectedCommon = new CommonData();
		// routings = new Vector();
		if (getSelectedCommons().size() > 0) {
			selectedCommon = getSelectedCommons().get(0);
			// routings = selectedCommon.getRoutings();
		}

		return null;
	}

	private void generateVariantList() {
		variantList = new Vector();
		VariantReportDAO dao = new VariantReportDAO();
		List<VariantReport> variants = dao.findAll();

		SelectItem selectItem = new SelectItem(":0:"
				+ Formatting.leadingChar("", '-', 41), "");
		variantList.addElement(selectItem);
		for (int i = 0; i < variants.size(); i++) {
			VariantReport aVariant = variants.get(i);
			selectItem = new SelectItem(aVariant.getName() + ":" + (i + 1)
					+ ":" + aVariant.getSelectField(), aVariant.getName());
			variantList.addElement(selectItem);
		}
	}

	private String generateSelectFieldText() {
		String value = "";
		if (this.isSelectCustomer())
			value += "X";
		else
			value += "-";
		if (this.isSelectCustomerName())
			value += "X";
		else
			value += "-";
		if (this.isSelectCustomerPoNo())
			value += "X";
		else
			value += "-";
		if (this.isSelectSaleOrder())
			value += "X";
		else
			value += "-";
		if (this.isSelectSaleLine())
			value += "X";
		else
			value += "-";
		if (this.isSelectChannel())
			value += "X";
		else
			value += "-";
		if (this.isSelectSaleCreated())
			value += "X";
		else
			value += "-";
		if (this.isSelectPreDue())
			value += "X";
		else
			value += "-";
		if (this.isSelectDueDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectPirNo())
			value += "X";
		else
			value += "-";
		if (this.isSelectPirDueDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectMaterial())
			value += "X";
		else
			value += "-";
		if (this.isSelectDesignFg())
			value += "X";
		else
			value += "-";
		if (this.isSelectDyeLot())
			value += "X";
		else
			value += "-";
		if (this.isSelectQuantity())
			value += "X";
		else
			value += "-";
		if (this.isSelectUom())
			value += "X";
		else
			value += "-";
		if (this.isSelectSeq())
			value += "X";
		else
			value += "-";
		if (this.isSelectProdCreateDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectPlanStartDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectPlanFinishDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectStartDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectLastPostingDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectPoNo())
			value += "X";
		else
			value += "-";
		if (this.isSelectDesignGreige())
			value += "X";
		else
			value += "-";
		if (this.isSelectGreigeDue())
			value += "X";
		else
			value += "-";
		if (this.isSelectPlanGreige())
			value += "X";
		else
			value += "-";
		if (this.isSelectActualGreige())
			value += "X";
		else
			value += "-";
		if (this.isSelectGreigeRemark())
			value += "X";
		else
			value += "-";
		if (this.isSelectStartDye())
			value += "X";
		else
			value += "-";
		if (this.isSelectDyeRemark())
			value += "X";
		else
			value += "-";
		if (this.isSelectDueSubmitDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectSubmitDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectStatus())
			value += "X";
		else
			value += "-";
		if (this.isSelectPlanDueDate())
			value += "X";
		else
			value += "-";
		if (this.isSelectCurrentOper())
			value += "X";
		else
			value += "-";
		if (this.isSelectUserStatus())
			value += "X";
		else
			value += "-";
		if (this.isSelectTotalProduct())
			value += "X";
		else
			value += "-";
		if (this.isSelectFinishGoods())
			value += "X";
		else
			value += "-";
		if (this.isSelectConfirmToPlan())
			value += "X";
		else
			value += "-";
		if (this.isSelectConfirmToSale())
			value += "X";
		else
			value += "-";
		if (this.isSelectConfirmType())
			value += "X";
		else
			value += "-";
		return value;
	}

	private void convertVariantToField(String value) {
		this.setSelectCustomer(setSelectFromFlag(value, 0));
		this.setSelectCustomerName(setSelectFromFlag(value, 1));
		this.setSelectCustomerPoNo(setSelectFromFlag(value, 2));
		this.setSelectSaleOrder(setSelectFromFlag(value, 3));
		this.setSelectSaleLine(setSelectFromFlag(value, 4));
		this.setSelectChannel(setSelectFromFlag(value, 5));
		this.setSelectSaleCreated(setSelectFromFlag(value, 6));
		this.setSelectPreDue(setSelectFromFlag(value, 7));
		this.setSelectDueDate(setSelectFromFlag(value, 8));
		this.setSelectPirNo(setSelectFromFlag(value, 9));
		this.setSelectPirDueDate(setSelectFromFlag(value, 10));
		this.setSelectMaterial(setSelectFromFlag(value, 11));
		this.setSelectDesignFg(setSelectFromFlag(value, 12));
		this.setSelectDyeLot(setSelectFromFlag(value, 13));
		this.setSelectQuantity(setSelectFromFlag(value, 14));
		this.setSelectUom(setSelectFromFlag(value, 15));
		this.setSelectSeq(setSelectFromFlag(value, 16));
		this.setSelectProdCreateDate(setSelectFromFlag(value, 17));
		this.setSelectPlanStartDate(setSelectFromFlag(value, 18));
		this.setSelectPlanFinishDate(setSelectFromFlag(value, 19));
		this.setSelectStartDate(setSelectFromFlag(value, 20));
		this.setSelectLastPostingDate(setSelectFromFlag(value, 21));
		this.setSelectPoNo(setSelectFromFlag(value, 22));
		this.setSelectDesignGreige(setSelectFromFlag(value, 23));
		this.setSelectGreigeDue(setSelectFromFlag(value, 24));
		this.setSelectPlanGreige(setSelectFromFlag(value, 25));
		this.setSelectActualGreige(setSelectFromFlag(value, 26));
		this.setSelectGreigeRemark(setSelectFromFlag(value, 27));
		this.setSelectStartDye(setSelectFromFlag(value, 28));
		this.setSelectDyeRemark(setSelectFromFlag(value, 29));
		this.setSelectDueSubmitDate(setSelectFromFlag(value, 30));
		this.setSelectSubmitDate(setSelectFromFlag(value, 31));
		this.setSelectStatus(setSelectFromFlag(value, 32));
		this.setSelectPlanDueDate(setSelectFromFlag(value, 33));
		this.setSelectCurrentOper(setSelectFromFlag(value, 34));
		this.setSelectUserStatus(setSelectFromFlag(value, 35));
		this.setSelectTotalProduct(setSelectFromFlag(value, 36));
		this.setSelectFinishGoods(setSelectFromFlag(value, 37));
		this.setSelectConfirmToPlan(setSelectFromFlag(value, 38));
		this.setSelectConfirmToSale(setSelectFromFlag(value, 39));
		this.setSelectConfirmType(setSelectFromFlag(value, 40));
	}

	private boolean setSelectFromFlag(String value, int pos) {
		try {
			if (value.charAt(pos) == 'X')
				return true;
			else
				return false;
		} catch (StringIndexOutOfBoundsException e) {
			System.out.println(e.toString());
			return false;
		}
	}

	private ResourceBundle getMessageBundle() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ResourceBundle rb = ResourceBundle.getBundle(
				"com.tpc.control.message.ResourceBundle", facesContext
						.getViewRoot().getLocale());
		return rb;
	}

	private void clearSaleForm() {
		customerFrom = "";
		customerTo = "";
		saleOrderFrom = "";
		saleOrderTo = "";
		customerPoFrom = "";
		customerPoTo = "";
		saleDueDateFrom = null;
		saleDueDateTo = null;
	}

	private void clearPirForm() {
		pirNoFrom = "";
		pirNoTo = "";
		pirDueDateFrom = null;
		pirDueDateTo = null;
	}

	public void selectSaleEvent(ActionEvent event) {
		if (selectSale) {
			selectPir = !selectSale;
			clearPirForm();
		} else {
			clearSaleForm();
		}
	}

	public void selectPirEvent(ActionEvent event) {
		if (selectPir) {
			selectSale = !selectPir;
			clearSaleForm();
		} else {
			clearPirForm();
		}
	}

	public void changeVariantEvent(ActionEvent event) {
		String[] aName = selectVariantName.split(":");
		convertVariantToField(aName[2]);
		// variantName = aName[0];

	}

	public void showSaleEvent(ActionEvent event) {
		//System.out.println("showSaleEvent: "+selectedCommon.getSaleOrder()+":"+selectedCommon.getSaleItem());
		//System.out.println("showSaleEvent: "
		//		+ selectedCommon.getSaleData().getId().getSaleOrder() + ":"
		//		+ selectedCommon.getSaleData().getId().getSaleItem());
		// CommonDataDAO commonDataDao = new CommonDataDAO();
		// List<CommonData> saleProductions =
		// commonDataDao.findBySale(selectedCommon.getSaleData().getId().getSaleOrder(),
		// selectedCommon.getSaleData().getId().getSaleItem());
		//System.out.println("saleProductions Size: "
		//		+ selectedCommon.getSaleData().getCommonDatas().size());
		System.out.println("showSaleEvent: "+selectedCommon.getSaleData().getId().getSaleOrder()+":"+selectedCommon.getSaleData().getId().getSaleItem());
		selectSaleCommonData = new CommonData();
		totSaleProdLine     = saleCommonDatas.size();
		totSaleProdQty      = new Double(0);
		totSaleProdWipQty   = new Double(0);
		totSaleProdGoodsQty = new Double(0);
		for (int i=0;i<saleCommonDatas.size();i++) {
			CommonData aCommon = saleCommonDatas.get(i);
			totSaleProdQty      += aCommon.getQuantity();
			totSaleProdWipQty   += aCommon.getTotProduceQty();
			totSaleProdGoodsQty += aCommon.getFinishGoods();
		}
	}

	public void takeSelectionEvent(ActionEvent event) {
		System.out.println("takeSelectionEvent");
	}

	// public void showRoutingEvent(ActionEvent event) {
	// String prodNo =
	// (String)event.getComponent().getAttributes().get("productionNo");
	// System.out.println("prodNo: "+prodNo);
	// }
}